package com.starlight.flower_server.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.starlight.flower_server.controller.authentication.AuthenticationCheck;
import com.starlight.flower_server.controller.exception.BadRequestException;
import com.starlight.flower_server.domain.TotalData;
import com.starlight.flower_server.domain.item.cqrs.ItemPageQuery;
import com.starlight.flower_server.domain.item.cqrs.ItemPageQueryByCategoryId;
import com.starlight.flower_server.domain.item.data.ItemData;
import com.starlight.flower_server.infrastructure.FlowerServerResponse;
import com.starlight.flower_server.infrastructure.RoleType;
import com.starlight.flower_server.infrastructure.Status;
import com.starlight.flower_server.infrastructure.entity.Item;
import com.starlight.flower_server.domain.service.ItemService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author yfy2001
 * @date 2023/4/5 15:08
 * 商品接口
 */
@RestController
@RequestMapping("/item")
public class ItemController {
    @Resource
    private ItemService itemService;

    @GetMapping("/getItemDetailById")
    public FlowerServerResponse<Item> getItemDetailById(Long id){
        return FlowerServerResponse.success(itemService.queryItemDetailById(id));
    }

    @PostMapping("/deleteItemById")
    @AuthenticationCheck(roleType = {RoleType.ROOT})
    public FlowerServerResponse<Integer> deleteItemById(Long id){
        return FlowerServerResponse.success(itemService.logicDeleteItemById(id));
    }

    @PostMapping("/createItem")
    public FlowerServerResponse<Integer> createItem(@RequestBody Item item){
        if (item.getItemCategoryId().equals(0L)){
            //商品分类只放子分类id
            throw new BadRequestException(Status.CATEGORY_ERROR.getMessage(),Status.CATEGORY_ERROR);
        }
        return FlowerServerResponse.success(itemService.insertItem(item));
    }

    @PostMapping("/updateItem")
    public FlowerServerResponse<Integer> updateItem(@RequestBody Item item){
        return FlowerServerResponse.success(itemService.modifyItem(item));
    }

    @GetMapping("/getItemPage")
    public FlowerServerResponse<IPage<ItemData>> getItemPage(ItemPageQuery query){
        return FlowerServerResponse.success(itemService.queryItemList(query));
    }

    @GetMapping("/getAllItemData")
    public FlowerServerResponse<TotalData<ItemData>> getAllItemData(){
        return FlowerServerResponse.success(itemService.queryAllItemData());
    }

    /**
     * 根据分类分页查询
     * 需要分类id，分页参数
     * @param query
     * @return
     */
    @GetMapping("/getItemPageByCategoryId")
    public FlowerServerResponse<IPage<ItemData>> getItemPageByCategoryId(ItemPageQueryByCategoryId query){
        if (query.getCategoryId()==null){
            throw new BadRequestException(Status.CATEGORY_ERROR.getMessage(),Status.CATEGORY_ERROR);
        }
        return FlowerServerResponse.success(itemService.queryItemPageByCategoryId(query));
    }

    @GetMapping("/getItemListByCategoryId")
    public FlowerServerResponse<List<ItemData>> getItemListByCategoryId(Long categoryId){
        return FlowerServerResponse.success(itemService.queryItemListByCategoryId(categoryId));
    }

    @GetMapping("/getItemTotalCount")
    public FlowerServerResponse<TotalData<ItemData>> getItemTotalCount(){
        return FlowerServerResponse.success(itemService.queryItemTotalCount());
    }

    @GetMapping("/getRandomItemList")
    public FlowerServerResponse<List<ItemData>> getRandomItemList(Integer count){
        if (count==null){
            throw new BadRequestException(Status.PARAMETER_ERROR.getMessage(), Status.PARAMETER_ERROR);
        }
        return FlowerServerResponse.success(itemService.queryRandomItemList(count));
    }
}
